package jee.boot.oauth.standard.rest;

import jee.boot.common.basic.Page;
import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.common.basic.result.Result;
import jee.boot.entity.auth.StandardModel;
import jee.boot.entity.auth.standard.DataValue;
import jee.boot.oauth.standard.service.StandardModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 模型的标准统一入口
 */
@RestController
@RequestMapping("/standardModel/{modelCode}")
public class StandardModelRestController {

    @Autowired
    private StandardModelService service;

    /**
     * 查看
     *
     * @param data
     * @return
     */
    @PostMapping("/get")
    public ResponseMessage<Map<String, DataValue>> get(@PathVariable("modelCode") String modelCode, @RequestBody Map<String, DataValue> data) {
        StandardModel entity=new StandardModel();
        entity.setModel(modelCode);
        entity.setData(data);
        entity = service.get(entity);
        return Result.success(entity.getData());
    }

    /**
     * 列表
     *
     * @param entity
     * @return
     */
    @PostMapping("/list")
    public ResponseMessage<Page<Map<String, DataValue>>> list(@PathVariable("modelCode") String modelCode, @RequestBody StandardModel entity) {
        entity.setModel(modelCode);
        if (entity.getPage() == null) {
            entity.setPage(new Page<>());
        }
        Page<Map<String, DataValue>> findPage = service.findPage(entity);
        return Result.success(findPage);
    }

    /**
     * 新增
     *
     * @param data
     * @return
     */
    @PostMapping("/save")
    public ResponseMessage<StandardModel> save(@PathVariable("modelCode") String modelCode, @RequestBody Map<String, DataValue> data) {
        StandardModel entity=new StandardModel();
        entity.setModel(modelCode);
        entity.setData(data);
        service.save(entity);
        return Result.success(entity);
    }


    /**
     * 修改
     *
     * @param data
     * @return
     */
    @PostMapping("/update")
    public ResponseMessage<StandardModel> update(@PathVariable("modelCode") String modelCode, @RequestBody Map<String, DataValue> data) {
        StandardModel entity=new StandardModel();
        entity.setModel(modelCode);
        entity.setData(data);
        service.update(entity);
        return Result.success(entity);
    }


    /**
     * 删除
     *
     * @param data
     * @return
     */
    @PostMapping("/delete")
    public ResponseMessage<StandardModel> delete(@PathVariable("modelCode") String modelCode, @RequestBody Map<String, DataValue> data) {
        StandardModel entity=new StandardModel();
        entity.setModel(modelCode);
        entity.setData(data);
        service.delete(entity);
        return Result.success();
    }
    /**
     * 导出
     *
     * @param entity
     * @return
     */
    @PostMapping("/export")
    public ResponseMessage<StandardModel> export(@PathVariable("modelCode") String modelCode, @RequestBody StandardModel entity) {
        entity.setModel(modelCode);
        if (entity.getPage() == null) {
            entity.setPage(new Page<>());
        }
        service.exportData(entity);
        return Result.success();
    }
}
